﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Graphs
{
    public class SymbolGraph
    {
        private Dictionary<string, int> st;//符号名 -> 索引
        private string[] keys;//索引 -> 符号名
        private Graph G;

        public SymbolGraph(string fileName, string sp)
        {
            var strs = File.ReadAllLines(fileName);
            st = new Dictionary<string, int>();

            //第一遍
            foreach (var str in strs)
            {
                var _strs = str.Split(sp);
                foreach (var _str in _strs)
                {
                    st.Add(_str,st.Count);
                }
            }

            keys = new string[st.Count];
            foreach (var name in st.Keys)
            {
                keys[st[name]] = name;
            }

            //第二遍 将每一行的第一个顶点和该行的其他顶点相连
            foreach (var str in strs)
            {
                var _strs = str.Split(sp);
                int v = st[_strs[0]];
                for (var i = 1; i < _strs.Length; i++)
                {
                    G.AddEdge(v,st[_strs[i]]);
                }
            }
        }

        public bool Contains(string s)
        {
            return st.ContainsKey(s);
        }

        public int Index(string s)
        {
            return st[s];
        }

        public string Name(int v)
        {
            return keys[v];
        }

        public Graph Gra()
        {
            return G;
        }
    }
}
